package com.firebird.app.kotlin.stock.trend.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.firebird.app.kotlin.stock.trend.activity.HSAStockActivity
import com.firebird.app.kotlin.stock.trend.activity.QuotationActivity
import com.firebird.app.kotlin.stock.trend.adapter.StockRankListViewAdapter
import com.firebird.app.kotlin.stock.trend.bean.Stock
import com.firebird.app.kotlin.stock.trend.service.StockService
import com.firebird.app.kotlin.stock.trend.service.UIListener
import com.firebird.app.kotlin.stock.trend.view.NoScrollListView
import com.firebird.app.kotlin.stock.trend.view.ObservableScrollView
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.activity.StockDetailActivity
import com.firebird.app.kotlin.stock.trend.utils.Constant

/**
 * 市场行情界面中，显示沪深涨跌排行榜的Fragment
 * @author ljk
 * @date   2019年10月28日
 */
class FragmentQuotationGg(context: Context) : Fragment(), UIListener {
    //private ScrollView scrollView;
    private var scrollView: ObservableScrollView? = null
    private var nslv_zfb: NoScrollListView? = null
    private var tv_zfb: TextView? = null
    private var zfbTitle: RelativeLayout? = null
    private var parentActivity: QuotationActivity? = null
    //private ArrayList<Stock> stockList;
//public static List<Stock> stockList;
    private var listViewAdapter: StockRankListViewAdapter? = null
    private var zfbIsClose = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //parentActivity = activity as QuotationActivity?
        //StockService.getQuotationRank(0, 10, Constant.STOCK_COLUMN_NOWPRICE, Constant.ORDER_BY_DESC, 1002, this)
    }

    @SuppressLint("InflateParams")
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        scrollView = inflater.inflate(R.layout.fragment_quotation_hs, null) as ObservableScrollView?
        zfbTitle = scrollView!!.findViewById<View>(R.id.rl_hs_zfb) as RelativeLayout
        tv_zfb = scrollView!!.findViewById<View>(R.id.tv_zfb as Int) as TextView
        nslv_zfb = scrollView!!.findViewById<View>(R.id.lv_hs_zfb) as NoScrollListView
        listViewAdapter = StockRankListViewAdapter(context!!)
        nslv_zfb!!.setAdapter(listViewAdapter)
        nslv_zfb!!.setOnClickListener { v ->
            val tv_name = v.findViewById<View>(R.id.stock_name) as TextView
            println("itme " + v.id + " stock name " + tv_name.text)
            val intent = Intent(context, StockDetailActivity::class.java)
            intent.putExtra("code", tv_name.text)
            context!!.startActivity(intent)
        }
        return scrollView
    }

    override fun onStart() {
        super.onStart()
        //处理ScrollView滚动时，导航栏的显示与隐藏
/*scrollView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if(event.getAction() == MotionEvent.ACTION_MOVE){

					if(event.getY()-scrollView.downY>10){
						//下滑
						parentActivity.tabWidgetIsVisable(true);
					}else {
						//上滑
						if(scrollView.getScrollY() - scrollView.scrollDownY > 10){
							parentActivity.tabWidgetIsVisable(false);
						}
					}

					System.out.println("scroll oy"+scrollView.scrollDownY+" ny="+scrollView.getScrollY());
				}
				v.performClick();
				return false;
			}
		});
		*/zfbTitle!!.setOnClickListener {
            if (zfbIsClose) {
                val drawable: Drawable = resources.getDrawable(R.mipmap.label_expand)
                /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
                tv_zfb!!.setCompoundDrawables(drawable, null, null, null)
                nslv_zfb!!.visibility = View.VISIBLE
                zfbIsClose = false
            } else {
                val drawable: Drawable = resources.getDrawable(R.mipmap.label_close)
                /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
                tv_zfb!!.setCompoundDrawables(drawable, null, null, null)
                nslv_zfb!!.visibility = View.GONE
                zfbIsClose = true
            }
        }
        val zfbMore = zfbTitle!!.findViewById<View>(R.id.hs_more) as TextView
        zfbMore.setOnClickListener {
            val intent = Intent(context, HSAStockActivity::class.java)
            startActivity(intent)
        }
    }

    override fun refresh(vararg params: Any?) {
        println("come in")
        val stockList = params[0] as List<Stock>
        if (stockList != null) {
            for (s in stockList) {
                println("=========>>股票名称：" + s.name + " 股票代码：" + s.code + " 现价：" + s.nowPrice)
            }
        }
        listViewAdapter!!.setDate(stockList)
        listViewAdapter!!.notifyDataSetChanged()
        //listViewAdapter.notifyDataSetInvalidated();
    }

}
